﻿using System;
using System.Reflection;

namespace Easy.Common.RegisterType
{
    public class RegisterTypeInfo : IEquatable<RegisterTypeInfo>
    {
        public Assembly Assembly { get; private set; }
        public Type BaseType { get; private set; }
        public Type ImplementationType { get; private set; }
        public IocLifeTime IocLifetime { get; private set; }
        public InterceptorType InterceptorType { get; private set; }


        public RegisterTypeInfo(Type baseType, RegisterTypeAttribute registerTypeAttribute)
        {
            CheckHelper.NotNull(baseType, "baseType");

            CheckHelper.NotNull(registerTypeAttribute, "registerTypeAttribute");

            if (!baseType.IsAssignableFrom(registerTypeAttribute.ImplementationType))
            {
                throw new ArgumentException(string.Format(Resource.NotAssignableFrom, registerTypeAttribute.ImplementationType.Name, baseType.Name), "registerTypeAttribute.ImplementationType");
            }


            BaseType = baseType;
            ImplementationType = registerTypeAttribute.ImplementationType;
            IocLifetime = registerTypeAttribute.IocLifetime;
            InterceptorType = registerTypeAttribute.InterceptorType;
            Assembly = baseType.Assembly;
        }


        public RegisterTypeInfo(Type baseType, Type implementationType, IocLifeTime iocLifetime = IocLifeTime.New, InterceptorType interceptorType = InterceptorType.None)
        {
            CheckHelper.NotNull(baseType, "baseType");

            CheckHelper.NotNull(implementationType, "implementationType");

            if (!baseType.IsAssignableFrom(implementationType))
            {
                throw new ArgumentException(string.Format(Resource.NotAssignableFrom, implementationType.Name, baseType.Name), "implementationType");
            }


            BaseType = baseType;
            ImplementationType = implementationType;
            IocLifetime = iocLifetime;
            InterceptorType = interceptorType;
            Assembly = baseType.Assembly;
        }

        public bool Equals(RegisterTypeInfo other)
        {
            if (Type.Equals(this.BaseType, other.BaseType)
                && Type.Equals(this.ImplementationType, other.ImplementationType)
                && this.IocLifetime == other.IocLifetime
                && this.InterceptorType == other.InterceptorType
                && Assembly.Equals(this.Assembly, other.Assembly))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
